package com.ship.dispatch.bpm.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@title DynamicParamUtils
 *@description
 *@author yl
 *@create 2023/12/16 0016
 */

public class TemplateParamUtils {
    public static final String DYNAMIC_PARAM_PREFIX = "${";
    public static final String DYNAMIC_PARAM_SUFFIX = "}";
    public static final String DYNAMIC_PARAM_PREFIX2 = "${";
    public static final String DYNAMIC_PARAM_SUFFIX2 = "}";
    // 当data不是k-v形式对象时，写入第一个变量
    public static String processDynamicParamWithSingleParam(String content, Object data) {
        int begin = content.indexOf(DYNAMIC_PARAM_PREFIX);
        int end = content.indexOf(DYNAMIC_PARAM_SUFFIX);
        if (begin == -1 || end == -1 || begin > end) {
            return content;
        }
        String substring = content.substring(begin, end + 1);
        return content.replace(substring, data.toString());
    }

    // 多变量，data 为 key - value 形式
    public static String processDynamicParams(String content, Map<String,String> data) {
        int begin = content.indexOf(DYNAMIC_PARAM_PREFIX);
        int end = content.indexOf(DYNAMIC_PARAM_SUFFIX);
        if (begin == -1 || end == -1 || begin > end) {
            return content;
        }
        String startContent = content.substring(0, begin);
        String substring = content.substring(begin, end+1);
        if (StringUtils.isNotEmpty(substring)) {
            String key = substring.replace("${", "").replace("}", "");
            Object value = data.get(key);
            if (value != null) {
                startContent = startContent + value.toString();
            } else {
                startContent = startContent + "  ";
            }
        }

        int length = content.length();
        content = content.substring(end + 1, length);
        begin = content.indexOf(DYNAMIC_PARAM_PREFIX);
        end = content.indexOf(DYNAMIC_PARAM_SUFFIX);
        if (begin == -1 || end == -1 || begin > end) {
            return startContent+content;
        }
        return startContent + processDynamicParams(content, data);
    }

    /**
     * 获取模板类所有数据标签key
     * @param content
     * @param labels
     * @return
     */
    public static List<String> templateLabel(String content,List<String> labels){
        int begin = content.indexOf(DYNAMIC_PARAM_PREFIX2);
        int end = content.indexOf(DYNAMIC_PARAM_SUFFIX2);
        if (begin == -1 || end == -1 || begin > end) {
            return labels;
        }
        String startContent = content.substring(0, begin);
        String substring = content.substring(begin, end + 1);
        if (StringUtils.isNotEmpty(substring)) {
            String key = substring.replace(DYNAMIC_PARAM_PREFIX2, "").replace(DYNAMIC_PARAM_SUFFIX2, "");
            labels.add(key);
        }
        int length = content.length();
        if ( length == end + 1) {
            return labels;
        } else {
            return templateLabel(content.substring(end + 1, length), labels);
        }
    }



    public static String processDynamicLabelParams2(String content, Map<String,String> data) {
        int begin = content.indexOf(DYNAMIC_PARAM_PREFIX2);
        int end = content.indexOf(DYNAMIC_PARAM_SUFFIX2);
        if (begin == -1 || end == -1 || begin > end) {
            return content;
        }
        String startContent = content.substring(0, begin);
        String substring = content.substring(begin, end+1);
        if (StringUtils.isNotEmpty(substring)) {
            String key = substring.replace(DYNAMIC_PARAM_PREFIX2, "").replace(DYNAMIC_PARAM_SUFFIX2, "");
            Object value = data.get(key);
            if (value != null) {
                startContent = startContent + value;
            } else {
                startContent = startContent + "";
            }
        }

        int length = content.length();
        content = content.substring(end + 1, length);
        begin = content.indexOf(DYNAMIC_PARAM_PREFIX2);
        end = content.indexOf(DYNAMIC_PARAM_SUFFIX2);
        if (begin == -1 || end == -1 || begin > end) {
            return startContent+content;
        }
        return startContent + processDynamicLabelParams2(content, data);
    }

    public static String processDynamicLabelParams(String content, Map<String,String> data) {
        int begin = content.indexOf(DYNAMIC_PARAM_PREFIX2);
        int end = content.indexOf(DYNAMIC_PARAM_SUFFIX2);
        if (begin == -1 || end == -1 || begin > end) {
            return content;
        }
        String startContent = content.substring(0, begin);
        String substring = content.substring(begin, end+1);
        if (StringUtils.isNotEmpty(substring)) {
            String key = substring.replace(DYNAMIC_PARAM_PREFIX2, "").replace(DYNAMIC_PARAM_SUFFIX2, "");
            Object value = data.get(key);
            if (value != null) {
                startContent = startContent + value.toString();
            } else {
                startContent +=  startContent + " ";
            }
        }

        int length = content.length();
        if ( length == end + 1) {
            return startContent;
        } else {
            return startContent + processDynamicLabelParams(content.substring(end + 1, length), data);
        }
    }

    public static void main(String[] args) {
        String template = "你好，<!test1!>" +"请你吃饭，吃什么呢？<!test2!>";
        Map<String,String> map = new HashMap<>();
        map.put("test1","124");
        map.put("test2","567");
        String label = processDynamicLabelParams(template,map);
        System.out.println(label);
    }

}
